package com.wh.wisdomsite.xbox.us.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.pojo.us.EmployeeIn;
import com.wh.wisdomsite.xbox.us.dao.EmployeeMapper;
import com.wh.wisdomsite.xbox.us.entity.Employee;
import com.wh.wisdomsite.xbox.us.service.IEmployeeService;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工ServiceImpl
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.us.service.impl
 * @author: 丁奕
 * @date: 2018-08-03 15:42
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@Service("employeeService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    private static final Log logger = LogFactory.getLog(EmployeeServiceImpl.class);

    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public Map<String, Object> getMapById(Serializable id) {
        return employeeMapper.getMapById(id);
    }

    @Override
    public Page<Map<String, Object>> getMapsPage(Page page, Wrapper<Employee> wrapper) {
        SqlHelper.fillWrapper(page, wrapper);
        page.setRecords(this.baseMapper.getMapsPage(page, wrapper));
        return page;
    }

    @Override
    public JSONArray getEmployeeList(EmployeeIn in) {
        JSONArray jsonArray = new JSONArray();
        try{
            //设置条件
            EntityWrapper<Employee> ewEmployee = new EntityWrapper<Employee>();
            ewEmployee.setEntity(new Employee());
            if(in.getCompanyId() != null){
                ewEmployee.eq("companyId",in.getCompanyId());
            }
            if(in.getOfficeId() != null){
                ewEmployee.eq("officeId",in.getOfficeId());
            }
            ewEmployee.eq("dataState","1");
            ewEmployee.orderBy("createDate",true);
            //根据条件获取所有员工数据
            List<Employee> allEmployeeList = employeeMapper.selectList(ewEmployee);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id","");
            jsonObject.put("text","--请选择--");
            jsonArray.put(jsonObject);
            for(Employee employee: allEmployeeList){
                jsonObject = new JSONObject();
                jsonObject.put("id",employee.getEmId());
                jsonObject.put("text",employee.getName());
                jsonArray.put(jsonObject);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return jsonArray;
    }

    @Override
    public Employee selectDetailById(String emId) {
        return employeeMapper.selectDetailById(emId);
    }

    @Override
    public Map<String, String> getEmpNamesByCompId(String compId) {
        Map<String, String> nameMap = new HashMap<>();
        if(StringUtils.isNotEmpty(compId)){
            if("1".equals(compId)){
                compId = null;
            }
            List<Map<String, Object>> emMapList = employeeMapper.getEmpNamesByCompId(compId);
            for(Map<String, Object> map: emMapList){
                String loginName = (String)map.get("loginName");
                String emName = "";
                if(null != map.get("emName")){
                    emName = (String)map.get("emName");
                }else{
                    emName = loginName;
                }
                nameMap.put(loginName,emName);
            }
        }
        return nameMap;
    }

}
